//
//  ViewController.m
//  0322练习网络获取图片
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()
@property (nonatomic,strong) NSArray *arr;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
//     不用线程，直接显示
//    for (int i=0; i<self.arr.count; i++)
//    {
//        // 网址字符串
//        NSString *urlStr = self.arr[i];
//        // 网址对象
//        NSURL *url = [NSURL URLWithString:urlStr];
//        // 获取数据
//        NSData *data = [NSData dataWithContentsOfURL:url];
//        // 图像对象
//        UIImage *image = [UIImage imageWithData:data];
//        // 图像视图
//        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
//        imgView.frame = CGRectMake((i/3)*kWidth/3,(i%3)*kHeight/3,kWidth/3,kHeight/3);
//        // 添加到视图
//        [self.view addSubview:imgView];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btn1Pressed:(id)sender {
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    dispatch_async(queue, ^{
        for (int i=0;i<self.arr.count;i++) {
            NSString *urlStr = self.arr[i];
            // 网址对象
            NSURL *url = [NSURL URLWithString:urlStr];
            // 获取数据
            NSData *data = [NSData dataWithContentsOfURL:url];
            // 图像对象
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                        // 图像视图
                        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
                        imgView.frame = CGRectMake((i%3)*100 ,(i/3)*100+150,100, 100);
                        // 添加到视图
                        [self.view addSubview:imgView];
                    });
        }
    });
}
- (IBAction)bt2Pressed:(id)sender {
    dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);// 自定义并发任务
    for (int i = 0; i<self.arr.count; i++) {
        // 网址字符串
        NSString *urlStr = self.arr[i];
        dispatch_async(queue, ^{
        // 网址对象
        NSURL *url = [NSURL URLWithString:urlStr];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            // 图像视图
            UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
            imgView.frame = CGRectMake((i%3)*100 ,(i/3)*100+150,100, 100);
            // 添加到视图
            [self.view addSubview:imgView];
            });
        });
    }
    
}
- (IBAction)bt3Pressed:(id)sender {
    [self performSelectorInBackground:@selector(doInBack) withObject:nil];
}
- (void)doInBack
{
    for (int i = 0;i<self.arr.count;i++)
    {
        // 网址字符串
        NSString *urlStr = self.arr[i];
        // 网址对象
        NSURL *url = [NSURL URLWithString:urlStr];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        
        [self performSelectorOnMainThread:@selector(addImageViewWithArr:) withObject:@[image,@(i)] waitUntilDone:NO];
    }
}

- (void)addImageViewWithArr:(NSArray *)arr
{
    UIImage *image = arr[0];
    int i = [arr[1] intValue];
    // 图像视图
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    imgView.frame = CGRectMake((i%3)*100 ,(i/3)*100+150,100, 100);
    // 添加到视图
    [self.view addSubview:imgView];

}

@end
